package picture;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.swing.text.html.FormSubmitEvent;
import javax.xml.crypto.Data;

import org.omg.CORBA.PUBLIC_MEMBER;

import net.iharder.Base64;

public class BaseServiceofPicture {
	

		public static void stringTopic(String picdata,String path)
		{
//			try {
//				byte [] data = decompress(Base64.decode(picdata) );
//				savefile(path, (data));
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			byte[] data = null;
			
			try {
				data = Base64.decode(picdata);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			savefile(path, data);
		}
	  
		public static String picTostring(String path)
		{
	//		byte[] data = compress(loadFile(path));  
			byte[] data = loadFile(path); 
			return new String(Base64.encodeBytesToBytes(data));
		}
	
	    /** 
	     * 加载本地文件,并转换为byte数组 
	     * @return 
	     */  
	    public static byte[] loadFile(String path) {  
	        File file = new File(path);    
	        FileInputStream fis = null;  
	        ByteArrayOutputStream baos = null;  
	        byte[] data = null ;  
	  
	        try {  
	            fis = new FileInputStream(file);  
	            baos = new ByteArrayOutputStream((int) file.length());  
	  
	            byte[] buffer = new byte[1024];  
	            int len = -1;  
	            while ((len = fis.read(buffer)) != -1) {  
	                baos.write(buffer, 0, len);  
	            }  
	              
	            data = baos.toByteArray() ;  
	  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                if (fis != null) {  
	                    fis.close();  
	                    fis = null;  
	                }  
	                  
	                baos.close() ;  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	          
	        return data ;  
	    }  
	      
	    /** 
	     * 对byte[]进行压缩 
	     *  
	     * @param 要压缩的数据 
	     * @return 压缩后的数据 
	     */  
	    public static byte[] compress(byte[] data) {  
	        System.out.println("compress --- before:" + data.length);  
	          
	        GZIPOutputStream gzip = null ;  
	        ByteArrayOutputStream baos = null ;  
	        byte[] newData = null ;
	          
	        try {  
	            baos = new ByteArrayOutputStream() ;  
	            gzip = new GZIPOutputStream(baos);  
	              
	            gzip.write(data);  
	            gzip.finish();  
	            gzip.flush();  
	              
	            newData = baos.toByteArray() ;  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                gzip.close();  
	                baos.close() ;  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	          
	        System.out.println("compress --- after:" + newData.length);  
	        return newData ;  
	    }  
	    
	    public static byte[] decompress(byte [] str)
	    {
    	
	    	ByteArrayOutputStream out = new ByteArrayOutputStream();   
	    	ByteArrayInputStream in = new ByteArrayInputStream(str);   
	    	GZIPInputStream gunzip = null ;
			try {
				gunzip = new GZIPInputStream(in);
				byte[] buffer = new byte[256];   
		    	int n;   
		    	while ((n = gunzip.read(buffer))>= 0) {   
		    	    out.write(buffer, 0, n);   
		    	}  
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}   
	    	 
	    	return out.toString().getBytes();   
	    	
	    }
	    
	    
	    public static void savefile(String path,byte [] data)
	    {
	    	System.out.println("-------------");
	    	FileOutputStream fos = null;
	    	try {
				fos = new FileOutputStream(path);
				fos.write(data);
				fos.flush();
				fos.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	

}
